

#import "HLoginSettingViewController.h"
#import "Networking.h"
#import <TMCache.h>
#import "AppDelegate.h"
#import "InitDbTable.h"
#import "AppServiceConfig.h"

@interface HLoginSettingViewController ()


@end

@implementation HLoginSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"服务器地址设置";
    [self.view addSubview:[self LoginScorollView] ];
    
    [self bindTextFieldData];
}


#pragma mark 登录相关的控件

-(UITextField*) LoginTextField{
    
    NSInteger inputWidth = ScreenWidth * 0.8 , inputHeight = 38;
    
    UITextField* LoginName = [[UITextField alloc]initWithFrame:CGRectMake(0, 0, inputWidth, inputHeight)];
    LoginName.returnKeyType = UIReturnKeyNext;
    LoginName.keyboardType = UIKeyboardTypeDefault;
    LoginName.center = self.view.center;
    LoginName.delegate = self;
    LoginName.backgroundColor = RGB(0xE0, 0xFF, 0xFF);
    LoginName.layer.borderWidth = 1;
    LoginName.layer.borderColor = [RGB(218, 218, 218)CGColor];
    LoginName.layer.cornerRadius = 5;
    LoginName.layer.masksToBounds = YES;
    //    LoginName.font = [[UIFont boldSystemFontOfSize:16.0];
    
    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(10, 0, 20, 20)];
    imagev.image = [UIImage imageNamed:@"密码(1)"];
    LoginName.leftView = imagev;
    LoginName.leftViewMode = UITextFieldViewModeAlways;
    
    return LoginName;
}


-(UIScrollView*) LoginScorollView{
    
    UIScrollView *mainview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, NavBarHeight, ScreenWidth, ScreenHeight)];
    
    self.SettingIPAddress = [self LoginTextField];
    self.SettingIPAddress.top = 30;
    self.SettingIPAddress.tag = 1;
    self.SettingIPAddress.placeholder = @"IP或域名";
    
    self.SettingPort = [self LoginTextField];
    self.SettingPort.top = self.SettingIPAddress.bottom + 30;
    self.SettingPort.returnKeyType = UIReturnKeyNext;
    self.SettingPort.secureTextEntry = NO;
    self.SettingPort.tag = 2;
    self.SettingPort.placeholder = @"端口号";
    
    self.SettingIIS = [self LoginTextField];
    self.SettingIIS.top = self.SettingPort.bottom + 30;
    self.SettingIIS.returnKeyType = UIReturnKeyDone;
    self.SettingIIS.secureTextEntry = NO;
    self.SettingIIS.tag = 3;
    self.SettingIIS.placeholder = @"IIS 应用程序（可为空）";
    
    
    
    UIButton *doSetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doSetBtn.frame = CGRectMake(0, 0, 100, 40);
    doSetBtn.top = self.SettingIIS.bottom + 30;
    doSetBtn.right =  self.SettingIIS.right;
    
    [doSetBtn setTitle:@"设置" forState:UIControlStateNormal];
    doSetBtn.titleLabel.font= [UIFont systemFontOfSize:20];
    [doSetBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [doSetBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [doSetBtn addTarget:self action:@selector(didSaveLoginSetting:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *doReSetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    doReSetBtn.frame = CGRectMake(0, 0, 100, 40);
    
    doReSetBtn.top = self.SettingIIS.bottom + 30;
    doReSetBtn.left = self.SettingIIS.left;
    
    [doReSetBtn setTitle:@"使用默认配置" forState:UIControlStateNormal];
    doReSetBtn.titleLabel.font= [UIFont systemFontOfSize:20];
    [doReSetBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [doReSetBtn setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [doReSetBtn addTarget:self action:@selector(didResetButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [doReSetBtn sizeToFit];
    
    mainview.delaysContentTouches = NO;
    
    [mainview addSubview:self.SettingIPAddress];
    [mainview addSubview:self.SettingPort];
    [mainview addSubview:self.SettingIIS];
    [mainview addSubview:doSetBtn];
    [mainview addSubview:doReSetBtn];
    
    return mainview;
}

- (IBAction)didResetButtonClicked:(id)sender{
    [self hideKeyborad];
    
    [AppServiceConfig ClearServiceUrl];
    [self alert:@"设置成功"];
    
    [self bindTextFieldData];
}

- (void)didSaveLoginSetting:(UIButton *)sender{
    
    [self hideKeyborad];
    
    NSString *ip = self.SettingIPAddress.text;
    NSString *port = self.SettingPort.text;
    NSString *iis = self.SettingIIS.text;
    
    if([ip isEqualToString: @""]){
        [AppServiceConfig ClearServiceUrl];
        [self alert:@"IP 不能为空"];
        return;
    }
    
    if([port isEqualToString: @""]){
        port = @"80";
    }
    
    if([iis isEqualToString: @""]){
        iis = @"";
    }
    
    
    AppServiceModel* config = [[AppServiceModel alloc]init];
    
    config.IPAddressOrDomainName = ip;
    config.Port = port;
    config.IISApplicationName = iis;
    
    [AppServiceConfig SetServiceUrl:config];
    
    [self alert:@"保存成功！"];
    
}

-(void) hideKeyborad{
    [self.SettingIPAddress resignFirstResponder];
    [self.SettingPort resignFirstResponder];
    [self.SettingIIS resignFirstResponder];
}

-(void) bindTextFieldData{
    AppServiceConfig *config = [[AppServiceConfig alloc]init];
    AppServiceModel *model = [config GetServiceConfigModel];
    self.SettingIPAddress.text = model.IPAddressOrDomainName;
    self.SettingPort.text =  model.Port;
    self.SettingIIS.text =  model.IISApplicationName;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark 文本框 委托

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.tag == 1){
        [self.SettingPort becomeFirstResponder];
    }
    else if(textField.tag == 2){
        [self.SettingIIS becomeFirstResponder];
    }
    else{
        [textField resignFirstResponder];
    }
    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [textField resignFirstResponder];
}
@end

